package com.ts.proj.json;


import com.ts.base.controller.BaseController;
import com.ts.common.constant.InsUpdDelFlagConstant;
import com.ts.proj.entity.TransportunitEntity;
import com.ts.proj.entity.excelEntity.TransportunitEntityExcel;
import com.ts.proj.entity.SystemUserEntity;
import com.ts.proj.service.TransportunitService;
import com.ts.common.constant.ConsoleConstant;
import com.ts.util.PageData;
import com.ts.util.StringsUtil;
import com.ts.util.DateUtil;
import com.ts.util.EasyPoiUtil;
import com.ts.util.EntityAttributeUtil;
import com.ts.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

//说明 字符串：Transportunit，作为替换字符串 LOWERTransportunit

@RestController
public class TransportunitJson extends BaseController{

    @Autowired
    private TransportunitService transportunitService;

    /**
     * 显示list
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value = "/transportunit-list/{page}/{size}")
    public Map<String,Object> transportunitList(@PathVariable("page") Integer page, @PathVariable("size") Integer size){
        PageData pageData = this.getPageData();
        pageData.put("deleteFlag", "N");
        long count = transportunitService.count(pageData);
        List<TransportunitEntity> transportunits = transportunitService.findAll(pageData,page-1,size);
        pageData.put("total",count);
        pageData.put("rows", transportunits);
        return pageData;
    }

    /**
     * 常量增删改
     * @return
     */
    @RequestMapping(value = "/transportunit-ins-upd-del/{id}")
    public Object logCreateUpdate(@PathVariable("id") String id){ 
	
		TransportunitEntity transportunit = transportunitService.findById(Integer.valueOf(id));
        PageData pageData = this.getPageData();

        List<String> strings = EntityAttributeUtil.entityAttribute(TransportunitEntity.class);
		 SystemUserEntity systemUserEntity = JsonUtils.jsonToObject(this.getRequest().getSession().getAttribute(ConsoleConstant.SESSIONUSER).toString(),SystemUserEntity.class);

        for(String attr :strings){
            //字段信息维护
            if(null!=pageData.get(attr)&&!"".equals(pageData.get(attr).toString())){
                //目前只能手动添加，这里添加所需要增加和修改的字段
                try {
                    Method[] methods = TransportunitEntity.class.getDeclaredMethods();
                    for(int i=0;i<methods.length;i++){
                        Method method = methods[i];
                        Class<?>[] returnType = method.getParameterTypes();
                        if(method.getName().contains(StringsUtil.upperCase(attr))&&!method.getName().contains("get")){
                            if (returnType[0].getName().equals("java.lang.Integer")){
                                method.invoke(transportunit, Integer.valueOf(pageData.getString(attr)));
                            }
                            else{
                                method.invoke(transportunit, pageData.getString(attr));
                            }
                        }
						if(method.getName().equals("setUserId")){
                            transportunit.setUserId(systemUserEntity.getId());
                        }
                        if(method.getName().equals("setBelongTo")){
                            transportunit.setBelongTo(systemUserEntity.getBelongTo());
                        }
						if(method.getName().equals("setPowerLevel")){
                            transportunit.setPowerLevel(systemUserEntity.getPowerLevel());
                        }

                    }

                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }  catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        }	
        transportunitService.save(transportunit);
        return null;
    } 
	@RequestMapping("/transportunit-download")
    public void export(HttpServletResponse response){
        PageData param = new PageData();
        param.put("deleteFlag", InsUpdDelFlagConstant.DELETE_FLAG_N);
        List<TransportunitEntity> entitys = transportunitService.findAll(param);
        List<TransportunitEntityExcel> excelEntities = new ArrayList<>();
        for(TransportunitEntity tempEntity :entitys){            
            TransportunitEntityExcel excel = new CopyObjectEntity2EntityExcel<TransportunitEntityExcel, TransportunitEntity>().copy(TransportunitEntityExcel.class,TransportunitEntity.class,tempEntity);
            excelEntities.add(excel);
        }
        EasyPoiUtil.exportExcel(excelEntities,"","",TransportunitEntityExcel.class, DateUtil.getID()+".xls",response);
    }	
}